package com.syni.mdd.yhd.common.all.controller.protect;

import com.syni.mdd.sjb.common.component.utils.common.annotations.CheckParams;
import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.sjb.common.component.utils.common.utils.AppConfig;
import com.syni.mdd.yhd.common.all.utils.DESUtil;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.sjb.common.component.utils.redis.utils.RedisTemplateUtils;;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.DependsOn;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @className AescEncryptController
 * @description TOO
 * @Author cfx
 * @DATE 2019/9/5 11:48
 * @VERSION 1.0
 **/
@RestController
@RequestMapping("/page")
@DependsOn("propertyLoadingListener")
@Log4j2
public class PageShareController {
    private static Map<Integer,Object> shareMap = new HashMap<>();
    //视频分享页
    private static final int VIDEO_PAGE = 1;
    //团购分享也
    private static final int GROUP_PAGE = 2;


    @PostConstruct
    public void init() {
        shareMap.put(VIDEO_PAGE,AppConfig.getProperty("video.share.page"));
        shareMap.put(GROUP_PAGE,AppConfig.getProperty("group.share.page"));
    }


    @PostMapping("/share")
    @CheckParams(params = {"id","shareType"})
    public Map<String,Object> share(@RequestBody Map<String,Object> paramsMap){
        try {
            Integer id = Integer.parseInt(paramsMap.get("id").toString());
            Integer shareType = Integer.parseInt(paramsMap.get("shareType").toString());
            if(shareMap.containsKey(shareType)){
                String url = shareMap.get(shareType).toString();
                String jumpUrl = String.format("%s%s",url,DESUtil.encrypt(id));
                return ResultMapHelper.success(jumpUrl, ResultMsg.MSG_GET_SUCCESS);
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return ResultMapHelper.failure(ResultMsg.ADMIN_ERROR);
    }
}
